package autoteam.tables;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import db.ColumnData;
import db.DBRecord;
import db.DataAccessObject;

public class UserTableModel extends AutoTeamTableModel
{
    
	private List<DBRecord> userRecords;
	private Map<Integer,String> colMap;
	private DataAccessObject dao;
	private int rowCount;
	
    public UserTableModel( )
    {
    	super( );
    	dao = new DataAccessObject();
    	userRecords = dao.getAllRecords("users");
    	colMap = new HashMap<Integer,String>();
    	colMap.put(0, "id");
    	colMap.put(1, "username");
    	colMap.put(2, "role");
    	colMap.put(3, "firstname");
    	colMap.put(4, "lastname");
    	rowCount = userRecords.size();
    	
    	
    }
    
    private static final long serialVersionUID = -3389518721252970867L;
    
    @ Override
    public void add( int j )
    {
    	// TODO Auto-generated method stub
    	rowCount++;
    	fireTableDataChanged();
    }
    
    @ Override
    public void delete( int r )
    {
    	// TODO Auto-generated method stub
    	ColumnData userToDelete = new ColumnData("id",getValueAt(r,0));
    	dao.deleteRecord("users", userToDelete);
    	rowCount--;
    	fireTableDataChanged();
    }
    
    @Override
    public String getColumnName(int col)
    {
    	return colMap.get(col);
    }
    
    @ Override
    public int getColumnCount( )
    {
		return 5;
    }
    
    @ Override
    public int getRowCount( )
    {
		return rowCount;
    }
    
    @ Override
    public Object getValueAt( int rowIndex, int colIndex)
    {
    	String colName = colMap.get(colIndex);
    	userRecords = dao.getAllRecords("users");
    	if (colIndex == 2){
    		Object record = userRecords.get(rowIndex).getFieldValue(colName);
    		String[] roleArray = {"", "Admin", "Athlete", "Fan"};
    		if (record == null || (Integer)record > 2){
    			return "";
    		}
    		return roleArray[(Integer)record + 1];
    	}
    		
    	return userRecords.get(rowIndex).getFieldValue(colName);
    }
    
}
